using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DrawUV : Graphic // : MaskableGraphic
{
    public Vector2[] points;
    public int[] triangles;
    public Vector2[] uv;
    public float lineWidth = 1f;
    public Color lineColor = Color.white;
    

    public void ResetPoints(Vector2[] points, int[] triangles)
    {
        this.points = points;
        this.triangles = triangles;
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();

        if (points == null || points.Length < 2)
            return;

        UIVertex vertex = UIVertex.simpleVert;
        vertex.color = lineColor;

        int pointCount = points.Length;

        for (int i = 0; i < pointCount - 1; i++)
        {
            Vector2 startPoint = points[i];
            Vector2 endPoint = points[i + 1];
            Vector2 direction = endPoint - startPoint;
            Vector2 perpendicular = new Vector2(-direction.y, direction.x).normalized;
            Vector2 halfWidthOffset = perpendicular * lineWidth * 0.5f;

            vertex.position = startPoint - halfWidthOffset;
            vh.AddVert(vertex);

            vertex.position = endPoint - halfWidthOffset;
            vh.AddVert(vertex);

            vertex.position = startPoint + halfWidthOffset;
            vh.AddVert(vertex);

            vertex.position = endPoint + halfWidthOffset;
            vh.AddVert(vertex);

            vh.AddTriangle(0 + 4 * i, 1 + 4 * i, 2 + 4 * i);
            vh.AddTriangle(2 + 4 * i, 1 + 4 * i, 3 + 4 * i);
        }

        for (int i = 0; i < triangles.Length;  i += 3)
        {

            
            vh.AddTriangle(triangles[i], triangles[i + 1], triangles[i + 2]);
        }
    }
}